package com.cjh.springcloud.controller;

import com.cjh.springcloud.pojo.Emp;
import com.cjh.springcloud.service.EmpService;
import org.assertj.core.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author cjh
 * @Date 2020/12/3 17:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/provider")
public class EmpController {

    @Autowired
    private EmpService empService;

    // 获取一些配置的信息，得到具体的微服务
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/queryOne/{empId}")
    public Emp queryOne(@PathVariable("empId") Integer empId) {
        Emp emp = empService.queryById(empId);
        return emp;
    }

    @GetMapping("/queryAll")
    public List<Emp> queryAll() {
        List<Emp> emps = empService.selectAllEmps();
        return emps;
    }

    @GetMapping("/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        // 得到一个具体的微服务信息，通过具体的微服务id，application name
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-EMP");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getUri() + "\t" +
                            instance.getServiceId()
            );
        }

        return client;
    }
}
